// LeftView.cpp : implementation of the CLeftView class
//

#include "stdafx.h"
#include "DialogTool.h"
#include "DialogDoc.h"
#include "LeftView.h"
#include "ChildFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLeftView

IMPLEMENT_DYNCREATE(CLeftView, CTreeView)

BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
	//{{AFX_MSG_MAP(CLeftView)
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelChanged)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLeftView construction/destruction

CLeftView::CLeftView()
{
}

CLeftView::~CLeftView()
{
}

BOOL CLeftView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CLeftView drawing

void CLeftView::OnDraw(CDC* pDC)
{
	CDialogDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
}


void CLeftView::OnInitialUpdate()
{
	CTreeView::OnInitialUpdate();

	CTreeCtrl& tree = GetTreeCtrl();

	tree.ModifyStyle(0, TVS_HASBUTTONS|TVS_SHOWSELALWAYS|TVS_EDITLABELS);

	HTREEITEM hRoot = tree.GetRootItem();

	CDialogDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	list<DIALOG*>::iterator it;
	it = pDoc->m_Dialogs.begin();
	while(it != pDoc->m_Dialogs.end())
	{
		DIALOG* pDialog = *it++;

		HTREEITEM hDialog = tree.InsertItem(pDialog->szName, hRoot);
		tree.SetItemData(hDialog, (DWORD)pDialog);

		list<DITEM*>::iterator lit;
		lit = pDialog->ItemList.begin();
		while(lit != pDialog->ItemList.end())
		{
			DITEM* pItem = *lit++;

			HTREEITEM hItem = tree.InsertItem(pItem->szName, hDialog);
			tree.SetItemData(hItem, (DWORD)pItem);
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CLeftView diagnostics

#ifdef _DEBUG
void CLeftView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CLeftView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

CDialogDoc* CLeftView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDialogDoc)));
	return (CDialogDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLeftView message handlers

void CLeftView::OnSelect(HTREEITEM hSelect)
{
	CDialogDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	CChildFrame* pFrameWnd = (CChildFrame*)GetParentFrame();
	if(pFrameWnd)
	{
		CDialogView* pDialogView = pFrameWnd->GetDialogView();
		if(pDialogView)
		{
			if(hSelect)
			{
				CTreeCtrl& tree = GetTreeCtrl();
				TDATA* pData = (TDATA*)tree.GetItemData(hSelect);
				if(pData)
				{
					switch(pData->nType)
					{
					case CDialogDoc::tItem:
						{
							HTREEITEM hParent = tree.GetParentItem(hSelect);
							DIALOG* pDialog = (DIALOG*)tree.GetItemData(hParent);
							if(pDialog && pDialog->nType == CDialogDoc::tDialog)
							{
								if(pDialog != pDialogView->m_pSelDlg)
								{
									pDialogView->m_pSelDlg = pDialog;
									pDialogView->Invalidate();
								}
							}
						}
						break;
					case CDialogDoc::tDialog:
						{
							DIALOG* pDialog = (DIALOG*)pData;
							if(pDialog != pDialogView->m_pSelDlg)
							{
								pDialogView->m_pSelDlg = pDialog;
								pDialogView->Invalidate();
							}
						}
						break;
					}
				}
			}
		}
	}
}

void CLeftView::OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

	HTREEITEM hSelect = pNMTreeView->itemNew.hItem;

	OnSelect(hSelect);
	
	*pResult = 0;
}
